package com.journaldev.spring.dao;

import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.journaldev.spring.model.Patient;
import com.journaldev.spring.model.PersonalOffice;
import com.journaldev.spring.model.UserInfo;

@Transactional
public class UserInfoDAOImpl implements UserInfoDAO {
	
	private static final Logger logger = LoggerFactory.getLogger(DistrictDAOImpl.class);

	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void addUserInfo(UserInfo p) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();
		session.persist(p);
		logger.info("User info saved successfully, User Details="+p);
	}

	@Override
	public void updateUserInfo(UserInfo p) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();
		session.update(p);
		logger.info("User info updated successfully, User Details="+p);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserInfo> listUser() {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();
		List<UserInfo> userList = session.createQuery("from UserInfo").list();
		for(UserInfo p : userList){
			logger.info("User List::"+p);
		}
		return userList;
	}

	@Override
	public UserInfo getUserInfoById(int id) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();		
		UserInfo p = (UserInfo) session.get(UserInfo.class, new Integer(id));
		logger.info("User loaded successfully, User info details="+p);
		return p;
	}

	@Override
	public void removeUserInfo(int id) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();
		UserInfo p = (UserInfo) session.get(UserInfo.class, new Integer(id));
		if(null != p){
			session.delete(p);
		}
		logger.info("User deleted successfully, User details="+p);
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserInfo getUserByNameAndPass(String userName, String password) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.getCurrentSession();
		Query query = session.createQuery("from UserInfo where userName=:usrname and passWord=:pwd");
		query.setParameter("usrname", userName);
		query.setParameter("pwd", password);
		List<UserInfo> userList = query.list();
		if(userList != null && !userList.isEmpty()) {
			return userList.get(0);
		}
		return null;
	}

	@Override
	public Set<Patient> getListPatient(int doctorId) {
		// TODO Auto-generated method stub
		UserInfo usrInfo = this.getUserInfoById(doctorId);
		if(usrInfo != null) {
			return usrInfo.getPatents();
		}
		return null;
	}

}
